package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Prod;
import com.powernode.domain.Sku;
import com.powernode.model.ChangeStock;
import com.powernode.model.Result;
import com.powernode.service.ProdService;
import com.powernode.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 商品业务控制层
 */
@Api(tags = "商品接口管理")
@RequestMapping("prod/prod")
@RestController
public class ProdController {

    private static final Logger log = LoggerFactory.getLogger(ProdController.class);
    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;


    @ApiOperation("多条件分页查询商品")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(Page<Prod> page,Prod prod) {
        page = prodService.page(page,new LambdaQueryWrapper<Prod>()
                .eq(ObjectUtil.isNotNull(prod.getStatus()),Prod::getStatus,prod.getStatus())
                .like(StringUtils.hasText(prod.getProdName()),Prod::getProdName,prod.getProdName())
                .orderByDesc(Prod::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> saveProd(@RequestBody Prod prod) {
        Integer count = prodService.saveProd(prod);
        return Result.handle(count>0);
    }

    @ApiOperation("查询修改商品的详情信息")
    @GetMapping("info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> loadProdDetailInfo(@PathVariable Long prodId) {
        Prod prod = prodService.queryProdDetailInfoById(prodId);
        return Result.success(prod);
    }

    @ApiOperation("修改商品信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> modifyProd(@RequestBody Prod prod) {
        Integer count = prodService.modifyProd(prod);
        return Result.handle(count>0);
    }

    @ApiOperation("批量删除商品")
    @DeleteMapping("{prodIds}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public Result<String> removeProds(@PathVariable List<Long> prodIds) {
        return Result.handle(prodService.removeBatchByIds(prodIds));
    }

    ////////////////////////////////// feign 接口 /////////////////////////////
    @ApiOperation("根据商品id集合查询商品对象集合")
    @GetMapping("getProdListByIds")
    public Result<List<Prod>> getProdListByIds(@RequestParam List<Long> ids) {
        List<Prod> prods = prodService.listByIds(ids);
        return Result.success(prods);
    }

    @GetMapping("getProdListByCategoryIds")
    public Result<List<Prod>> getProdListByCategoryIds(@RequestParam List<Long> categoryIds) {
        List<Prod> list = prodService.list(new LambdaQueryWrapper<Prod>()
                .in(Prod::getCategoryId, categoryIds)
        );
        return Result.success(list);
    }

    @GetMapping("getSkuListBySkuIds")
    public Result<List<Sku>> getSkuListBySkuIds(@RequestParam List<Long> skuIds) {
        if (skuIds == null || skuIds.isEmpty()) {
            log.info("购物车没有数据");
            return Result.success(Collections.emptyList());
        }
        List<Sku> skuList = skuService.listByIds(skuIds);
        return Result.success(skuList);
    }

    @PostMapping("changeProdAndSkuStock")
    public Result<Boolean> changeProdAndSkuStock(@RequestBody ChangeStock changeStock) {
        Boolean flag = prodService.changeProdAndSkuStock(changeStock);
        return Result.success(flag);
    }

    //////////////////////////////// 微信小程序 数据接口 ///////////////////////////
//    prod/prod/prod/prodInfo?prodId=101
    @ApiOperation("查询小程序商品详情")
    @GetMapping("prod/prodInfo")
    public Result<Prod> loadWxProdInfo(@RequestParam Long prodId) {
        Prod prod = prodService.queryWxProdInfoById(prodId);
        return Result.success(prod);
    }

}
